home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / docs / linux-do / programm / lpg-0.4 / lpg-0 / LPG / examples / ipc / popen3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-01  |  982 b   |  47 lines

  1. /*****************************************************************************
  2.  Excerpt from "Linux Programmer's Guide - Chapter 6"
  3.  (C)opyright 1994-1995, Scott Burkett
  4.  ***************************************************************************** 
  5.  MODULE: popen3.c
  6.  *****************************************************************************/
  7.  
  8. #include <stdio.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     FILE *pipe_fp, *infile;
  13.     char readbuf[80];
  14.  
  15.     if( argc != 3) {
  16.         fprintf(stderr, "USAGE:  popen3 [command] [filename]\n");    
  17.         exit(1);
  18.     }
  19.  
  20.     /* Open up input file */
  21.     if (( infile = fopen(argv[2], "rt")) == NULL)
  22.     {
  23.         perror("fopen");
  24.         exit(1);    
  25.     }
  26.  
  27.     /* Create one way pipe line with call to popen() */
  28.     if (( pipe_fp = popen(argv[1], "w")) == NULL)
  29.     {
  30.         perror("popen");
  31.         exit(1);
  32.     }
  33.  
  34.     /* Processing loop */
  35.     do { 
  36.         fgets(readbuf, 80, infile);
  37.         if(feof(infile)) break;
  38.  
  39.         fputs(readbuf, pipe_fp);
  40.     } while(!feof(infile));
  41.  
  42.     fclose(infile);    
  43.     pclose(pipe_fp);
  44.  
  45.     return(0);
  46. }
  47.